package com.numberone.web.controller.yplgoods;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Random;

import com.github.pagehelper.PageInfo;
import com.numberone.system.qo.GoodsLikeQuery;
import com.numberone.system.util.JsonResult;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.numberone.common.annotation.Log;
import com.numberone.common.enums.BusinessType;
import com.numberone.system.domain.Goods;
import com.numberone.system.service.IGoodsService;
import com.numberone.framework.web.base.BaseController;
import com.numberone.common.page.TableDataInfo;
import com.numberone.common.base.AjaxResult;
import com.numberone.common.utils.poi.ExcelUtil;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

/**
 * 商品 信息操作处理
 * 
 * @author numberone
 * @date 2020-09-28
 */
@RestController
@RequestMapping("/api/goods")
public class GoodsApiController extends BaseController
{


	@Autowired
	private IGoodsService goodsService;


	@Value("${uploadUrl}")
	private String returnUrl;//返回路径
	@Value("${uploadPath}")
	private String path;//存储路径
	private static Random random = new Random();
	//上传文件
	@PostMapping("/multipart")
		public String uploadFile(HttpServletRequest request, MultipartFile mediaFile) {
		String result = "false";
		if (!mediaFile.isEmpty()&&!mediaFile.getOriginalFilename().equals("null")) {
			File targetFile = null;
			String url = "";//返回存储路径
			String fileName = mediaFile.getOriginalFilename();
			if (fileName != null && fileName != "") {
				String fileF = fileName.substring(fileName.lastIndexOf("."), fileName.length());//文件后缀
				fileName = new Date().getTime() + "_" + new Random().nextInt(1000) + fileF;//新的文件名
				//先判断文件是否存在
				SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
				String fileAdd = sdf.format(new Date());
				//获取文件夹路径
				File file1 = new File(path + "/" + fileAdd);
				//如果文件夹不存在则创建
				if (!file1.exists() && !file1.isDirectory()) {
					file1.mkdirs();
				}
				//将图片存入文件夹
				targetFile = new File(file1, fileName);
				try {
					//将上传的文件写到服务器上指定的文件。
					mediaFile.transferTo(targetFile);
					url = returnUrl + fileAdd + "/" + fileName;
					return url;
				} catch (Exception e) {
					e.printStackTrace();
					return "false";
				}
			}
		}
		return result;
	}

	/**
	 * 首页模糊查询,根据商品名字
	 * @param qo
	 *
	 * @param
	 * @return
	 */
	@PostMapping("/like")
	public Object likeGoods(GoodsLikeQuery qo){

		PageInfo pageInfo  = goodsService.likeByNameAndTypeInfoFristId(qo);

		if (pageInfo.getList().isEmpty()){
			return JsonResult.noGoods();

		}
		return JsonResult.success(pageInfo);
	}

	/**
	 * 查看商品详情
	 * @author 郭财源
	 * @param goodsId
	 * @return
	 */
	@PostMapping("/goodsInfo")
	public JsonResult GoodsInfo(Long goodsId)
	{
		Goods goods = goodsService.selectGoodsById(goodsId);
		//将商品图片封装到商品信息中
//		List<GoodsImg> goodsImgs = goodsImgService.selectGoodsImgByGoodsId(goodsId);
//		goods.setGoodsImgList(goodsImgs);
		return JsonResult.success(goods);
	}
}
